package cn.bluemobi.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.bluemobi.app.entity.output.Category;
import cn.bluemobi.app.entity.output.Commodity;
import cn.bluemobi.app.mapper.CategoryMapper;
import cn.bluemobi.app.mapper.CommodityMapper;
import cn.bluemobi.app.service.CategoryService;
import cn.bluemobi.common.core.dto.ListDto;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryMapper categoryMapper;

	@Autowired
	private CommodityMapper commodityMapper;

	@Override
	public ListDto<Category> findCategoryList() {
		ListDto<Category> dto = new ListDto<Category>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("level", "0");
		List<Category> categories = categoryMapper.findCategoryList(params);
		if (!CollectionUtils.isEmpty(categories)) {
			for (Category category : categories) {
				Map<String, Object> secondParams = new HashMap<String, Object>();
				secondParams.put("level", "1");
				secondParams.put("parent", category.getCategoryId());
				List<Category> secondCategories = categoryMapper
						.findCategoryList(secondParams);
				for (Category secondCategory : secondCategories) {
					Map<String, Object> commodityParams = new HashMap<>();
					commodityParams.put("secondCategoryId",
							secondCategory.getCategoryId());
					List<Commodity> commodityList = commodityMapper
							.findCommodityList(commodityParams);
					secondCategory.setCommodityList(commodityList);
				}
				category.setSecondCategoryList(secondCategories);
			}
			dto.setList(categories);
		}
		return dto;
	}
}
